﻿
namespace EasyStep.TestActions.Utils
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Drawing;
    using OpenQA.Selenium;
    using System.IO;
    using EasyStep.TestExecution.Interfaces;
    using EasyStep.TestExecution.Data;
    


    public class BrowserHelper
    {
        public static Image MakeScreenshot(IExecuteDataContext DataContext, TestStepResult data)
        {
            try
            {
                var wait = new OpenQA.Selenium.Support.UI.WebDriverWait(DataContext.BrowserInstance as IWebDriver, new TimeSpan(0, 15, 0));
                wait.Until(b => b.FindElement(By.TagName("html")));

                var screenshot = ((ITakesScreenshot)DataContext.BrowserInstance).GetScreenshot();
                //data.ExecutionLog.AppendLine("Converting screenshot data to image");
                using (var imageStream = new MemoryStream(screenshot.AsByteArray))
                {
                    data.Screenshot = System.Drawing.Image.FromStream(imageStream);
                }
            }
            catch (Exception ex)
            {
                DataContext.Log.Fatal(string.Format("Can't create Screenshot:{0}", DataContext.TestHistoryManager.Convert(data)), ex);
            }
            return data.Screenshot;
        }
    }
}
